package com.feeyin.radar.crm.controller;

import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.crm.form.CustomerLimitForm;
import com.feeyin.radar.crm.service.CustomerService;
import com.feeyin.radar.crm.service.ExceptionCustomerService;
import com.feeyin.radar.sys.model.Users;
import com.feeyin.radar.sys.service.AssessmentTypeService;
import com.feeyin.radar.sys.service.CommonSettingsService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.view.RedirectView;

public class ExceptionCustomerController extends MultiActionController
{
  private ExceptionCustomerService exceptionCustomerService;
  private CustomerService customerService;
  private CommonSettingsService commonSettingsService;
  private AssessmentTypeService assessmentTypeService;
  private String exceptionCustomer;
  private String selectPerson;
  private String selectMoveTarget;

  public ExceptionCustomerService getExceptionCustomerService()
  {
    return this.exceptionCustomerService;
  }

  public void setExceptionCustomerService(ExceptionCustomerService exceptionCustomerService)
  {
    this.exceptionCustomerService = exceptionCustomerService;
  }

  public CustomerService getCustomerService() {
    return this.customerService;
  }

  public void setCustomerService(CustomerService customerService) {
    this.customerService = customerService;
  }

  public CommonSettingsService getCommonSettingsService() {
    return this.commonSettingsService;
  }

  public void setCommonSettingsService(CommonSettingsService commonSettingsService) {
    this.commonSettingsService = commonSettingsService;
  }

  public AssessmentTypeService getAssessmentTypeService() {
    return this.assessmentTypeService;
  }

  public void setAssessmentTypeService(AssessmentTypeService assessmentTypeService) {
    this.assessmentTypeService = assessmentTypeService;
  }

  public String getExceptionCustomer() {
    return this.exceptionCustomer;
  }

  public void setExceptionCustomer(String exceptionCustomer) {
    this.exceptionCustomer = exceptionCustomer;
  }

  public String getSelectPerson() {
    return this.selectPerson;
  }

  public void setSelectPerson(String selectPerson) {
    this.selectPerson = selectPerson;
  }

  public String getSelectMoveTarget() {
    return this.selectMoveTarget;
  }

  public void setSelectMoveTarget(String selectMoveTarget) {
    this.selectMoveTarget = selectMoveTarget;
  }

  public ModelAndView display(HttpServletRequest request, HttpServletResponse response)
  {
    Map map = new HashMap();
    map.put("result", request.getParameter("result"));
    map.put("deleteResult", request.getParameter("deleteResult"));

    Integer userId = (Integer)request.getSession().getAttribute("userId");
    String sFlag = request.getParameter("sFlag");
    String salePerson = "";
    if ((!StringUtils.isEmpty(sFlag)) && ("1".equals(sFlag))) {
      salePerson = request.getParameter("salePerson");
    }
    map.put("sFlag", sFlag);
    map.put("salePerson", salePerson);

    Page page = new Page();
    page.getPageList(this.exceptionCustomerService.getPageExceptionCustomersCount(salePerson, userId).intValue(), null, request);
    map.put("customers", this.exceptionCustomerService.getPageExceptionCustomers(salePerson, userId, page));

    if (this.assessmentTypeService.isExpUser(userId)) {
      map.put("isExpUser", "1");
    }
    map.put("customerLimitForm", this.customerService.getCustomerLimit(userId));

    return new ModelAndView(getExceptionCustomer(), map);
  }

  public ModelAndView preSelectPerson(HttpServletRequest request, HttpServletResponse response)
  {
    Integer userId = (Integer)request.getSession().getAttribute("userId");
    Map map = new HashMap();

    List users = new ArrayList();
    users = this.exceptionCustomerService.getSalePersons(userId);
    if (users != null) {
      for (int i = 0; i < users.size(); i++) {
        Users user = new Users();
        user = (Users)users.get(i);
        CustomerLimitForm customerLimitForm = new CustomerLimitForm();
        customerLimitForm = this.customerService.getCustomerLimit(user.getId());
        user.setCustomerLimitForm(customerLimitForm);
        if (this.assessmentTypeService.isExpUser(user.getId())) {
          user.setIsExpUser("1");
        }
      }
    }
    map.put("users", users);

    return new ModelAndView(getSelectPerson(), map);
  }

  public ModelAndView salePersonToCustomer(HttpServletRequest request, HttpServletResponse response)
  {
    String salePersonId = request.getParameter("salePersonId");
    boolean result = false;
    int success = 0;

    String[] ids = request.getParameterValues("id");
    if (!StringUtils.isEmpty(salePersonId)) {
      result = this.exceptionCustomerService.salePersonToCustomer(ids, Integer.valueOf(Integer.parseInt(salePersonId)));
      if (result) success = 1;
    }

    return new ModelAndView(new RedirectView("./exceptionCustomer.do?action=display&result=" + success));
  }

  public ModelAndView preSelectMoveTarget(HttpServletRequest request, HttpServletResponse response)
  {
    Map model = new HashMap();
    model.put("isUseOpenSea", Integer.valueOf(this.commonSettingsService.isUseCommons()));
    model.put("isMoveToExceptionPool", Integer.valueOf(this.commonSettingsService.isMoveToExceptionPool()));
    return new ModelAndView(getSelectMoveTarget(), model);
  }

  public ModelAndView moveCustomerToTarget(HttpServletRequest request, HttpServletResponse response)
  {
    Integer userId = (Integer)request.getSession().getAttribute("userId");
    boolean result = false;
    int success = 0;

    String isTarget = request.getParameter("isTarget");
    String[] ids = request.getParameterValues("id");
    result = this.exceptionCustomerService.moveCustomerToTarget(ids, userId, isTarget);
    if (result) success = 2;

    return new ModelAndView(new RedirectView("./exceptionCustomer.do?action=display&result=" + success));
  }
}